﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Data.BusinessObjects;
using Zwms.Client.Helper;
using Zwms.Client.CommonUI;

namespace Zwms.Client.InfoSys
{
    public partial class SysAccountNewForm : BaseForm
    {
        #region 成员函数
        public SysAccount CurrentSysAccount { get; set; }
        #endregion

        #region 构造函数
        public SysAccountNewForm()
        {
            InitializeComponent();
            this.CurrentSysAccount = new SysAccount();
            this.sysDepartmentBindingSource.DataSource = null;
            this.sysDepartmentBindingSource.DataSource = CommonDataHelper.SysDepartmentList;
            this.sysRoleBindingSource.DataSource = null;
            this.sysRoleBindingSource.DataSource = CommonDataHelper.SysRoleList;
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitControl()
        { 
            DataTable dt = new DataTable();
            dt.Columns.Add("dtText");
            dt.Columns.Add("dtValue");
            dt.Rows.Add(new object[]{"启用","1"});
            dt.Rows.Add(new object[]{"禁用","0"});
            this.cbIsLocked.DataSource = dt;
            this.cbIsLocked.DisplayMember = "dtText";
            this.cbIsLocked.ValueMember = "dtValue";
        }
        #endregion

        #region 控件函数
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            //数据完整性检查
            bool bRet = true;
            if (string.IsNullOrWhiteSpace(this.tbUserCode.Text))
            {
                bRet = false;
                tbUserCode.BackColor = Color.Yellow;
            }
            if (string.IsNullOrWhiteSpace(this.tbUserName.Text))
            {
                bRet = false;
                tbUserName.BackColor = Color.Yellow;
            }
            if (string.IsNullOrWhiteSpace(this.tbPassword.Text))
            {
                bRet = false;
                tbPassword.BackColor = Color.Yellow;
            }
            if (string.IsNullOrWhiteSpace(this.tbPasswordCfm.Text))
            {
                bRet = false;
                tbPasswordCfm.BackColor = Color.Yellow;
            }
            if (string.IsNullOrWhiteSpace(tbsUserRole.GetTextBoxContent()))
            {
                bRet = false;
                tbsUserRole.SetTextBGColor(Color.Yellow);
            }
            //if (string.IsNullOrWhiteSpace(tbsDepartment.GetTextBoxContent()))
            //{
            //    bRet = false;
            //    tbsDepartment.SetTextBGColor(Color.Yellow);
            //}
            if (!bRet)
            {
                return;
            }
            if (this.tbPassword.Text.Trim().CompareTo(this.tbPasswordCfm.Text.Trim()) != 0)
            {
                MessageBox.Show("初始密码与确认密码不相符，请重新输入！");
                return;
            }

            if (MessageBox.Show("您确定要添加该用户吗？", "注意", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
            {
                SysAccount record = new SysAccount();
                record.Id = this.tbUserCode.Text.Trim();
                record.UserName = this.tbUserName.Text.Trim();
                record.UserRole = this.tbsUserRole.Tag.ToString();
                record.DepartmentCode = this.tbsDepartment.Tag.ToString();
                record.DepartmentName = this.tbsDepartment.GetTextBoxContent().Trim();
                record.WarehouseCode = this.tbsWarehouse.Tag.ToString();
                record.WarehouseName = this.tbsWarehouse.GetTextBoxContent().Trim();
                record.IsLocked = this.cbIsLocked.SelectedValue.ToString().Trim();
                record.Remark = this.tbRemark.Text.Trim();
               ServiceReferenceZwms.ServiceZwmsClient client1 = new ServiceReferenceZwms.ServiceZwmsClient();
                DateTime dtNow = client1.GetCurrentSystemTime();
                record.CreateDate = Helper.CommonHelper.GetDateString(dtNow); 
                record.CreateTime = Helper.CommonHelper.GetTimeString(dtNow);
                record.Remark = this.tbRemark.Text;
                record.Password = GetHashValue.Cryptography.GetHashValue(this.tbPassword.Text.Trim());
                ServiceReferenceZwms.ServiceZwmsClient sysClient = new ServiceReferenceZwms.ServiceZwmsClient();
                bRet = sysClient.AddSysAccount(record);
                if (bRet)
                {
                    MessageBox.Show("成功!");
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("添加用户失败，请检查用户代码是否已存在！");
                    return;
                }
                
            }
        
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }


        private void tbsUserRole_SelectButtonClick(object sender, EventArgs e)
        {
            SysRoleWnd uc = new SysRoleWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择角色");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsUserRole.Tag = uc.SelectedRecord.Id;
                tbsUserRole.SetTextBoxContent(uc.SelectedRecord.RoleName);
            }
        }

        private void tbsDepartment_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.DepartmentWnd uc = new InfoWms.DepartmentWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择部门");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsDepartment.Tag = uc.SelectedRecord.Id;
                tbsDepartment.SetTextBoxContent(uc.SelectedRecord.DepartmentName);
            }
        }

        private void tbsWarehouse_SelectButtonClick(object sender, EventArgs e)
        {
            InfoWms.WarehouseWnd uc = new InfoWms.WarehouseWnd();
            CommonUI.ContainerForm window = new CommonUI.ContainerForm(uc, "选择仓库");
            uc.SetQueryState();
            if (window.ShowDialog() == DialogResult.OK)
            {
                tbsWarehouse.Tag = uc.SelectedRecord.Id;
                tbsWarehouse.SetTextBoxContent(uc.SelectedRecord.WarehouseName);
            }
        }

        #endregion

    }
}
